package com.liucxu.zimuzu.View.Fragement;

import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;

import com.liucxu.zimuzu.Base.BaseFragment;
import com.liucxu.zimuzu.Model.Bean.HotBean;
import com.liucxu.zimuzu.Model.Bean.HotEntity;
import com.liucxu.zimuzu.Presenter.IHotPresenter;
import com.liucxu.zimuzu.Presenter.Impl.HotPresenterImpl;
import com.liucxu.zimuzu.R;
import com.liucxu.zimuzu.View.Adapter.HotAdapter;
import com.liucxu.zimuzu.View.IView.IHotView;

import java.util.ArrayList;

/**
 * 今日热门排行Fragment
 * Created by liukanshan on 2016/12/9.
 */

public class HotFragment extends BaseFragment implements IHotView {

    private SwipeRefreshLayout swipe_refresh;
    private ListView content_list;
    private IHotPresenter presenterImpl;
    private HotAdapter hotAdapter;
    private ArrayList<HotEntity> entities=new ArrayList<>();
    private String channel;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot;
    }

    @Override
    public void setupView() {
        swipe_refresh = $(R.id.swipe_refresh);
        swipe_refresh.setColorSchemeColors(Color.parseColor("#0f88eb"));
        content_list = $(R.id.content_list);
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                setupData();
            }
        });
        hotAdapter = new HotAdapter(getActivity()
                , entities);
        content_list.setAdapter(hotAdapter);
    }

    @Override
    public void setupData() {
        channel = getArguments().getString("channel");
        presenterImpl = new HotPresenterImpl(this);
        presenterImpl.getTopHot(channel);
    }

    @Override
    public void setHots(HotBean beans) {
        if(beans.getData()!=null){
            entities.clear();
            entities.addAll(beans.getData());
            hotAdapter.setData(entities);
        }
    }

    @Override
    public void showLoading() {
        swipe_refresh.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipe_refresh.setRefreshing(false);
    }

    @Override
    public void showError(String errMsg) {
        Snackbar.make(swipe_refresh,errMsg,Snackbar.LENGTH_SHORT).show();
    }
}
